package com.ruoyi.core.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.core.domain.ClientProductUser;
import com.ruoyi.core.service.IClientProductUserService;
import com.ruoyi.core.vo.ClientProductUserCreateOperationVO;
import com.ruoyi.core.vo.ClientProductUserUpdateOperationVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * client-product-userController
 *
 * @author ruoyi
 * @date 2020-09-28
 */
@Api("客户端内置用户信息管理")
@RestController
@RequestMapping("/client-product-users")
public class ClientProductUserController extends BaseController {
    @Autowired
    private IClientProductUserService clientProductUserService;

    /**
     * 查询client-product-user列表
     */
    @ApiOperation("客户端内置用户明细列表")
    @PreAuthorize("@ss.hasRole('admin')")
    @GetMapping("/{productId}/list")
    public TableDataInfo list(@PathVariable("productId") Long productId) {
        startPage();
        List<ClientProductUser> list = clientProductUserService.findByProductId(productId);
        return getDataTable(list);
    }

    //    /**
    //     * 导出client-product-user列表
    //     */
    //    @PreAuthorize("@ss.hasPermi('client-product-user:export')")
    //    @Log(title = "client-product-user", businessType = BusinessType.EXPORT)
    //    @GetMapping("/export")
    //    public AjaxResult export(ClientProductUser clientProductUser)
    //    {
    //        List<ClientProductUser> list = clientProductUserService.selectClientProductUserList(clientProductUser);
    //        ExcelUtil<ClientProductUser> util = new ExcelUtil<ClientProductUser>(ClientProductUser.class);
    //        return util.exportExcel(list, "client-product-user");
    //    }

    //    /**
    //     * 获取client-product-user详细信息
    //     */
    //    @PreAuthorize("@ss.hasPermi('client-product-user:query')")
    //    @GetMapping(value = "/{id}")
    //    public AjaxResult getInfo(@PathVariable("id") Long id)
    //    {
    //        return AjaxResult.success(clientProductUserService.selectClientProductUserById(id));
    //    }

    /**
     * 新增client-product-user
     */
    @Log(title = "内置账户管理", businessType = BusinessType.INSERT)
    @ApiOperation("新增内置账户")
    @PreAuthorize("@ss.hasRole('admin')")
    @PostMapping
    public AjaxResult add(@RequestBody ClientProductUserCreateOperationVO clientProductUserCreateVO) {
        try {
            return toAjax(clientProductUserService.add(clientProductUserCreateVO));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 修改client-product-user
     */
    @Log(title = "内置账户管理", businessType = BusinessType.UPDATE)
    @ApiOperation("更新内置账户")
    @PreAuthorize("@ss.hasRole('admin')")
    @PutMapping
    public AjaxResult edit(@RequestBody ClientProductUserUpdateOperationVO clientProductUserUpdateVO) {
        try {
            return toAjax(clientProductUserService.update(clientProductUserUpdateVO));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }

    }

    /**
     * 删除client-product-user
     */
    @PreAuthorize("@ss.hasPermi('client-product-user:remove')")
    @Log(title = "client-product-user", businessType = BusinessType.DELETE)
    @ApiOperation("删除内置账户")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable("ids") Long[] ids) {
        return toAjax(clientProductUserService.deleteClientProductUserByIds(ids));
    }

    /**
     * 随机获取指定产品用户信息
     */
    @ApiOperation("获取客户端内置账户信息")
    @GetMapping("/{productId}")
    public AjaxResult getInfo(@PathVariable("productId") Long productId) {

        return AjaxResult.success(clientProductUserService.getClientProductUserRandomStrategy(productId));
    }


    @ApiOperation("获取客户端内置账户信息")
    @GetMapping("/getJsPluginInfo")
    public AjaxResult getJsPluginInfo() {
        return AjaxResult.success(clientProductUserService.getClientProductUserRandomStrategyByJsPlugin());
    }

    @GetMapping(value = "productCookieList")
    @ApiModelProperty("获取cookie列表-描述")
    public TableDataInfo getProductCookieList(){
        return getDataTable(clientProductUserService.productList());
    }
}
